package com.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.javabean.User;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:jdbc.properties")
//加载类路径下指定properties配置文件
//等价于<context:property-placeholder location="classpath:jdbc.properties"/>

@Import({SpringConfig.class})
//将其他注解配置类导入当期注解配置类中
//等价于 <import resource="classpath:spring-comment.xml"></import>
public class DataSourceConfiguration {
    //声明属性保存jdbc.properties取出的数据
    //通过@Value与${key}进行赋值
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

//    <bean id="u1" class="com.javabean.User">
//        <property name="id" value="1"/>
//        <property name="name" value="zhangsan"/>
//    </bean>
    @Bean("u1")
    public User getUser(){
        return new User(2,"zhangsan2222");
    }

    @Bean("c3p0")
    public ComboPooledDataSource getc3p0() throws Exception {
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return  dataSource;
    }

    @Bean("druid")
    public DruidDataSource getDruid(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }





}
